<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Agent Controller
 *
 * @package     VisualQ Local System
 * @author      CSU OJT Students '10 - '11 (Michael Roland B. Martin)
 * @copyright   Copyright (c) 2010 - 2011, Bitstop Network Services, Inc.
 * @version     1.0 January 27, 2011
 */

class Agent extends Controller {

    function Agent()
    {
        parent::Controller();

        $this->lang->load('local', 'English');
        //$this->output->enable_profiler(TRUE);

        // Check if user is logged in
        if($this->session->userdata('id'))
        {
            if(strcmp($this->session->userdata('account_type'), 'manager') == 0)
            {
                redirect(base_url() . 'manager/');
            }
        }
        else
        {
            redirect(base_url() . 'login/');
        }

        // Check if unit is registered
        if( ! $this->session->userdata('unit_name'))
        {
            redirect(base_url() . 'login/');
        }
    }

    function index()
    {
        $this->load->view('agent_close_view');
    }

    function widget()
    {
        // Get settings
        $query = $this->db->get('settings');
        if($query->num_rows > 0)
        {
            foreach($query->result_array() as $row)
            {
                $data['settings'][$row['name']] = $row['value'];
            }
        }
        $query = $this->db->get_where('accounts', array('id' => $this->session->userdata('id')));
        $row = $query->row_array();
        $data['settings']['last_name'] = $row['last_name'];
        $data['settings']['first_name'] = $row['first_name'];
        $this->load->view('agent_widget_view', $data);
    }

    /**
     * Build the agents list flexigrid
     *
     * NOTE: The method is based on the codeigniter implementation prototype
     */
    function logs_grid($type = 'waiting')
    {
        $this->load->helper('flexigrid');

        // Set the columns and column properties
        $colModel = array(
            'id'            => array($this->lang->line('agent_id'),             20,     TRUE,   'center',   2),
            'service'       => array($this->lang->line('agent_service_tite'),   250,    TRUE,   'left',     0),
            'issue_time'    => array($this->lang->line('agent_issue_time'),     100,    TRUE,   'center',   1),
            'queue_number'  => array($this->lang->line('agent_queue_no'),       40,     TRUE,   'center',   1)
        );

        // Set default flexigrid title
        $title = $this->lang->line('agent_queue_waiting');

        // Set or overwrite additional flexigrid properties according to the value of type
        if(strcmp($type, 'served') == 0)
        {
            $colModel['timestart']          = array($this->lang->line('agent_process_start'), 100,    TRUE,   'center', 2);
            $colModel['timeend']            = array($this->lang->line('agent_process_end'),   100,    TRUE,   'center', 2);
            $colModel['processing_time']    = array($this->lang->line('agent_total_time'),    70,     TRUE,   'center', 2);
            $colModel['closing_code']       = array($this->lang->line('agent_closing_code'),  150,    TRUE,   'left',   2);

            $title = $this->lang->line('agent_queue_served');
        }
        elseif(strcmp($type, 'missed') == 0)
        {
            $colModel['action']             = array('Action', 100, TRUE, 'right', 0);
            $title = $this->lang->line('agent_queue_missed');
        }

        // Set the flexigrid properties
        $gridParams = array(
            'width'                 => 'auto',
            'height'                => 400,
            'rp'                    => 15,
            'rpOptions'             => '[10,15,20,25,40]',
            'pagestat'              => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity'          => 0.5,
            'title'                 => $title,
            'showTableToggleBtn'    => FALSE
        );

        // Build flexigrid js
        $grid_js = build_grid_js('flexigrid', site_url("ajax/logs_grid/" . $type), $colModel, 'id', 'asc', $gridParams);
        $data['js_grid'] = $grid_js;

        $this->load->view('log_view', $data);
    }

}

/* End of file agent.php */
/* Location: ./system/application/controllers/agent.php */